package edu.predatorwars.server.host;

import java.util.UUID;
import java.util.List;
import edu.predatorwars.server.engine.GameEngine;
import edu.predatorwars.server.engine.GameListener;
import edu.predatorwars.shared.models.*;


public class GameInstance implements GameListener {

	private GameHostImpl host;
	private UUID gameId; 
	private GameEngine engine;
	
	public GameInstance(GameHostImpl host, UUID gameId, GameEngine engine) {
		this.host = host; //Needs reference to host in order to update it
		this.gameId = gameId; //unique identifier for the game
		this.engine = engine;
	}

	public UUID getUUID() {
		return gameId;
	}
	
	public List<Player> getPlayerList() {
		return engine.getCurrentPlayers();
	}
	
	public GameEngine getGameEngine() {
		return engine;
	}

	
	public void boardChanged(GameEngine engine, Board board) throws Exception {
		//create message payload, redirect to host
		
		//assertion: engine.getCurrentPlayer.STATUS == ALIVE
		if(engine.getCurrentPlayer().getPlayerStatus() != PlayerStatus.ALIVE) {
			throw new Exception("Player whose turn it is must be alive");
		}
		BoardPayload payload = new BoardPayload(board, engine.getCurrentPlayers(), engine.getCurrentPlayer(), gameId);
		host.latestUpdate(payload, engine.getCurrentPlayers());
	}
	
	
	public void gameStateChanged(GameEngine engine, int boardState) {
		//create message payload, redirect to host
		if (boardState == 1) {
			//character selection stage, different payload type
			CharacterListPayload payload = new CharacterListPayload(engine.getAvailableCreatures(), gameId);
			host.latestUpdate(payload, engine.getCurrentPlayers());
		} else {
			StateChangePayload payload = new StateChangePayload(boardState, engine.getCurrentPlayers(), gameId);
			host.latestUpdate(payload, engine.getCurrentPlayers());
		}
	}
}
